package com.erp.erp_project.controller;

import com.erp.erp_project.core.annotation.Permission;
import com.erp.erp_project.core.base.AjaxResult;
import com.erp.erp_project.core.exception.CustomException;
import com.erp.erp_project.model.*;
import com.erp.erp_project.service.MenuService;
import com.erp.erp_project.service.PowerService;
import com.erp.erp_project.service.SubmenuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.annotation.Documented;
import java.util.*;

/**
 * @author yh
 */
@Controller
@AllArgsConstructor
@RequestMapping("/menu")
public class MenuController {
    private final MenuService menuService;
    private final SubmenuService submenuService;
    private final PowerService powerService;

    @RequestMapping("/index")
    public String index(Model model, HttpServletRequest request){
        HttpSession session=request.getSession(true);
        List<Power> powers=(List<Power>) session.getAttribute("showpowers");
        User user=(User) session.getAttribute("user");
        if(user.getRole().getRoleName().equals("超级管理员")){
            Menu menu=new Menu();
            List<Menu> menus=menuService.list(menu);
            Submenu submenu=new Submenu();
            List<Submenu> submenus=submenuService.list(submenu);
            model.addAttribute("menus",menus);
            model.addAttribute("submenus",submenus);
        }else {
            if(powers!=null){
                Set<String> names=new HashSet<>();
                for (Power power:powers){
                    names.add(power.getParent());
                }
                List<Submenu> submenus=submenuService.selectnames(names);
                Set<String> parentnames=new HashSet<>();
                for (Submenu sub:submenus){
                    parentnames.add(sub.getParent());
                }
                List<Menu> menus=menuService.selectnames(parentnames);



                model.addAttribute("menus",menus);
                model.addAttribute("submenus",submenus);
            }else {
                model.addAttribute("menus",null);
                model.addAttribute("submenus",null);
            }
        }

        return "index";
    }

    @GetMapping("/list")
    public String list(){
        return "menu/list";
    }

    @PostMapping("/list")
    @ResponseBody
    public AjaxResult<PageInfo<Submenu>> listrole(Submenu Submenu, Integer page, Integer limit, String menufield, String condition){
        PageHelper.startPage(page,limit);
        List<Submenu> submenus=menuService.relist(Submenu,menufield,condition);
        PageInfo<Submenu> pageInfo = new PageInfo<>(submenus);
        return new AjaxResult<>(pageInfo);
    }

    @GetMapping("/add")
    @Permission("菜单添加")
    public String add(){
        return "menu/add";
    }

    @PostMapping("/add")
    @ResponseBody
    @Permission("菜单添加")
    public AjaxResult addmenu(String powers, Submenu submenu) throws CustomException {
        menuService.addmenu(powers, submenu);
        return new AjaxResult("添加成功");
    }

    @PostMapping("/delete")
    @ResponseBody
    @Permission("菜单删除")
    public AjaxResult delete(String ids){
        menuService.delete(ids);
        return new AjaxResult("删除成功");
    }

    @GetMapping("/update")
    @Permission("菜单修改")
    public String update(Model model,Integer id){
        Submenu upsubmenu=submenuService.selectById(id);
        Power power=new Power();
        power.setParent(upsubmenu.getName());
        List<Power> subpower=powerService.list(power);
        model.addAttribute("upsubmenu",upsubmenu);
        model.addAttribute("subpower",subpower);
        return "menu/update";
    }

    @PostMapping("/update")
    @ResponseBody
    @Permission("菜单修改")
    public AjaxResult updateMenu(Submenu submenu,String rename,String ins,String upids,String dpids,String newins) throws CustomException {
        menuService.updateMenu( submenu, rename, ins, upids, dpids, newins);
        return new AjaxResult("保存成功");
    }
}
